﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using QuanLyDuocPham.View.QuanLyHeThong.DanhMuc;
using QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc;
using System.Linq;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class BACSI_Control : IDisposable

    {

        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {
            
            cmb.Items.Clear();
            var types = db.BACSI_Get_List();
            var typeorder = types.OrderBy(w => w.HO_TEN);
            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_BACSI";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            
            BACSI_Get_ListResult temp = new BACSI_Get_ListResult();

            temp.MA_BACSI = "...";
            temp.HO_TEN = "Thêm mới";
            cmb.Items.Add(temp);
        }
        public void addData(string ma_bs, TextBox txtHo_Ten, ComboBox cmbChuyen_Khoa, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtD_Dong, TextBox txtTL_HoaHong, TextBox txtGhi_Chu)
        {
            if (validateData(txtHo_Ten, cmbChuyen_Khoa, txtDia_Chi))
            {               
                

                db.BACSI_Insert(
                    ma_bs,
                    txtHo_Ten.Text,
                    cmbChuyen_Khoa.Text,
                    txtDia_Chi.Text,
                    txtD_Thoai.Text,
                    txtD_Dong.Text,
                    double.Parse( txtTL_HoaHong.Text),                                
                    txtGhi_Chu.Text
                );

                System.Windows.Forms.MessageBox.Show("Đã thêm thành công!");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Dữ liệu không hợp lệ!");
            }
        }

        private bool validateData(TextBox txtHo_Ten, ComboBox cmbChuyen_Khoa, TextBox txtDia_Chi)
        {
            if (txtHo_Ten.Text.Trim() == "" || txtDia_Chi.Text.Trim() == "" ||
                cmbChuyen_Khoa.SelectedItem == null)
            {
                return false;
            }

            return true;
        }

        public void updateData(string ma_bs, TextBox txtHo_Ten, ComboBox cmbChuyen_Khoa, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtD_Dong, TextBox txtTL_HoaHong, TextBox txtGhi_Chu)
        {
            if (validateData(txtHo_Ten, cmbChuyen_Khoa, txtDia_Chi) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmbChuyen_Khoasl = (ComboBoxItem)cmbChuyen_Khoa.SelectedItem;

                db.BACSI_Update(ma_bs, ma_bs, txtHo_Ten.Text, cmbChuyen_Khoasl.Content.ToString(), txtDia_Chi.Text, txtD_Thoai.Text, txtD_Dong.Text, double.Parse(txtTL_HoaHong.Text), txtGhi_Chu.Text);
                MessageBox.Show("Đã cập nhật!");
            }
        }


        public void loadData(ListView List)
        {


            var bacsis = db.BACSI_Get_List();
            List.Items.Clear();
            var typeorder = bacsis.OrderBy(w => w.HO_TEN);
            foreach (var t in typeorder)
            {
                ListViewItem item = new ListViewItem();

                item.Content = t;
                item.ToolTip = createTooltip(t);
                List.Items.Add(item);
            }            
        }


        public void loadData(DataPager dp)
        {

            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();

            var bacsis = db.BACSI_Get_List();
            
            var typeorder = bacsis.OrderBy(w => w.HO_TEN);
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.HO_TEN.Trim() !="")
                {
                    dpSource.Add(new { STT = i, u.MA_BACSI, u.HO_TEN, u.CHUYEN_KHOA, u.DI_DONG, u.DIA_CHI, u.DIEN_THOAI, u.TL_HOAHONG });
                    i++;

                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        private Grid createTooltip(BACSI_Get_ListResult g)
        {
            Grid gr = new Grid();



            //Ten Bac Si
            TextBlock tbTen = new TextBlock();
            tbTen.FontSize = 16;
            tbTen.Width = 120;
            tbTen.FontWeight = FontWeights.Bold;
            tbTen.Foreground = new BrushConverter().ConvertFromString("#FF116A98") as SolidColorBrush;
            tbTen.Height = 20;
            tbTen.Text = g.HO_TEN;
            tbTen.Margin = new Thickness(140, 22, 10, 0);
            tbTen.TextAlignment = TextAlignment.Center;
            tbTen.TextWrapping = TextWrapping.Wrap;
            tbTen.VerticalAlignment = VerticalAlignment.Top;

            //Chuyen Khoa              
            TextBlock tbChuyen_Khoa = new TextBlock();
            tbChuyen_Khoa.Height = 20;
            tbChuyen_Khoa.Width = 100;
            tbChuyen_Khoa.HorizontalAlignment = HorizontalAlignment.Right;
            tbChuyen_Khoa.FontSize = 12;
            tbChuyen_Khoa.FontWeight = FontWeights.Bold;
            tbChuyen_Khoa.Text = g.CHUYEN_KHOA;
            tbChuyen_Khoa.Margin = new Thickness(0, 49, 10, 0);
            tbChuyen_Khoa.TextAlignment = TextAlignment.Left;
            tbChuyen_Khoa.VerticalAlignment = VerticalAlignment.Top;

            //Dia Chi            
            TextBlock tbDia_Chi = new TextBlock();
            tbDia_Chi.Height = 20;
            tbDia_Chi.Width = 150;
            tbDia_Chi.FontSize = 12;
            tbDia_Chi.FontWeight = FontWeights.Bold;
            tbDia_Chi.Margin = new Thickness(160, 49, 136, 0);
            tbDia_Chi.TextAlignment = TextAlignment.Left;
            tbDia_Chi.VerticalAlignment = VerticalAlignment.Top;
            tbDia_Chi.Text = g.DIA_CHI;

            //Dien Thoai
            TextBlock tbD_Thoai = new TextBlock();
            tbD_Thoai.Height = 20;
            tbD_Thoai.Width = 100;
            tbD_Thoai.FontSize = 12;
            tbD_Thoai.FontWeight = FontWeights.Bold;
            tbD_Thoai.FlowDirection = FlowDirection.RightToLeft;
            tbD_Thoai.Margin = new Thickness(0, 0, 0, 0);
            tbD_Thoai.TextAlignment = TextAlignment.Left;
            tbD_Thoai.VerticalAlignment = VerticalAlignment.Top;
            tbD_Thoai.HorizontalAlignment = HorizontalAlignment.Right;
            tbD_Thoai.Text = g.DIEN_THOAI;

            //Di Dong
            TextBlock tbD_Dong = new TextBlock();
            tbD_Dong.Height = 20;
            tbD_Dong.Width = 100;
            tbD_Dong.FontSize = 12;
            tbD_Dong.FontWeight = FontWeights.Bold;
            tbD_Dong.Margin = new Thickness(160, 76, 12, 0);
            tbD_Dong.VerticalAlignment = VerticalAlignment.Top;
            tbD_Dong.Text = g.DI_DONG;

            //Ti Le Hoa Hong
            TextBlock tbTL_HoaHong = new TextBlock();
            tbTL_HoaHong.Height = 20;
            tbTL_HoaHong.FontSize = 12;
            tbTL_HoaHong.Width = 100;
            tbTL_HoaHong.FontWeight = FontWeights.Bold;
            tbTL_HoaHong.Margin = new Thickness(160, 107, 12, 0);
            tbTL_HoaHong.VerticalAlignment = VerticalAlignment.Top;
            tbTL_HoaHong.Text = g.TL_HOAHONG.ToString();

            //Ghi Chu
            TextBlock tbGhi_Chu = new TextBlock();
            tbGhi_Chu.Height = 20;
            tbGhi_Chu.FontSize = 12;
            tbGhi_Chu.FontWeight = FontWeights.Bold;
            tbGhi_Chu.Margin = new Thickness(160, 134, 12, 0);
            tbGhi_Chu.VerticalAlignment = VerticalAlignment.Top;
            tbGhi_Chu.Text = g.GHI_CHU;

            gr.Children.Add(tbTen);
            gr.Children.Add(tbChuyen_Khoa);
            gr.Children.Add(tbDia_Chi);
            gr.Children.Add(tbD_Thoai);
            gr.Children.Add(tbD_Dong);
            gr.Children.Add(tbTL_HoaHong);
            gr.Children.Add(tbGhi_Chu);

            return gr;
        }

        public void loadID1_moi(TextBox txtID)
        {
            string ID = "BS";
            

            var data = db.BACSI_Get_List();
            if (data.Count() == 0)
            {
                ID = "BS0001";
            }
            else
            {
                string maxID = data.First().MA_BACSI;
                foreach (var t in data)
                {
                    int temp = Int32.Parse(t.MA_BACSI.Substring(0, 2));
                    if (db.BACSI_GetByMaBacsi("BS" + (temp + 1).ToString()).Count() == 0)
                    {
                        maxID = "BS" + (temp + 1).ToString();
                        break;
                    }
                    else
                    {
                        if (temp > Int32.Parse(maxID.Substring(0, 2)))
                            maxID = t.MA_BACSI;
                    }
                }
                for (int i = 0; i < (4 - (Int32.Parse(maxID.Substring(0, 2)) + 1).ToString().Length); i++)
                    ID = ID + "0";
                ID = ID + (Int32.Parse(maxID.Substring(0, 2)) + 1).ToString();
            }
            txtID.Text = ID;

        }
        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.BACSI_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "BS0001";
            }
            else
            {
                for (int i = 1; i <= (rowcount + 1); i++)

                {
                    string IDtam = "BS";
                    for(int j=0; j<(4-i.ToString().Length);j++)
                        IDtam=IDtam+"0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.BACSI_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_BACSI.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }
 
            }
            return ID;

        }
        public void loadForm_Edit(string ID_Edit,  TextBox txtHo_Ten, ComboBox cmbChuyen_Khoa, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtD_Dong, TextBox txtTL_HoaHong, TextBox txtGhi_Chu)
        {

            var sl = (BACSI_GetByMaBacsiResult)db.BACSI_GetByMaBacsi(ID_Edit).Single();


            
            
            txtHo_Ten.Text = sl.HO_TEN;
            int i = 0;
            foreach (var t in cmbChuyen_Khoa.Items)
            { 
                var cmbChuyen_Khoasl = (ComboBoxItem)t;
                if (cmbChuyen_Khoasl.Content.ToString().Trim() == sl.CHUYEN_KHOA.Trim())
                {
                    break;
                }
                i++;
            }
            cmbChuyen_Khoa.SelectedIndex = i;
            txtTL_HoaHong.Text = sl.TL_HOAHONG.ToString();
            txtDia_Chi.Text = sl.DIA_CHI;
            txtD_Thoai.Text = sl.DIEN_THOAI;
            txtD_Dong.Text = sl.DI_DONG;
            txtGhi_Chu.Text = sl.GHI_CHU;
            
        }


        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.DONTHUOC_GetByMaBacsi(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.BACSI_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;

        }

        
    }
}
